import axios from 'axios'
import * as constants from './constants'
import { fromJS } from 'immutable';

const setData = (result) => ({
    type: constants.SET_HOME_DATA,
    topPicList: result.topPicList,
    articleList: result.articleList,
    recommendList: result.recommendList
})

const setLoadMore = (result) => ({
    type: constants.SET_LOAD_MORE,
    articleList: fromJS(result),
})

const setWriterList = (result) => ({
    type: constants.SET_WRITER_DATA,
    writerDataList: fromJS(result),
})

export const getData = () => {
    return (dispatch) => {
        axios.get('/api/home.json').then(res => {
            let result = res.data.data
            dispatch(setData(result))
        }).catch(err => err)
    }
}

export const getLoadMore = () => {
    return (dispatch) => {
        axios.get('/api/homeList.json').then(res => {
            let result = res.data.data
            dispatch(setLoadMore(result))
        }).catch(err => err)
    }
}

export const toggleTopShow = (show) => ({
    type: constants.TOGGLE_TOP_SHOW,
    show
})

export const getWriterDataList = () => {
    return (dispatch) => {
        axios.get('/api/writerList.json').then(res => {
            let result = res.data.data
            dispatch(setWriterList(result))
        }).catch(err => err)
    }
}